package com.ebt.m.proposal_v2.mvp.presenter;

import android.content.Context;
import android.util.Log;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.widgets.BaseRxActivity;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.event.MainChooseEvent;
import com.ebt.m.proposal_v2.bean.temp.ActivityFinishStack;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.dao.response.ResponseMainDetail;
import com.ebt.m.proposal_v2.dao.response.ResponseTouDan;
import com.ebt.m.proposal_v2.dao.response.TouDanInfo;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseBodyBase;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.utils.JsonUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * MainProductAdapter->Presenter 处理点击主险选中实现
 *
 * @author archer.qi
 *         Created on 2017/5/26.
 */
public class MainItemViewPresenter {
    private Context mContext;
    private TouDanInfo touDanInfo;

    public MainItemViewPresenter(Context context) {
        mContext = context;
    }

    /**
     * 点击主险
     *
     */
    public void onItemClick(ResponseMain mainData, ParamGetRisks param) {
        int productId = 0;
        if (mainData != null){
            productId = mainData.productId;
        }
        if (productId == 0) {
            submitMainChoose(null);
            return;
        }
        touDanInfo = new TouDanInfo(mainData.isOpenInsurance, mainData.insuranceUrl,mainData.productId);
        getMainProductDetails(productId, param);
    }

    private void getMainProductDetails(int productId, ParamGetRisks param){
        if (param != null) {
            /*点击之后，请求主险参数*/
            getMainProductDetails(productId, DataUtils.formatRequestParamAge(param.age + "", param.birthday), param.sex, param.profession);
        } else {
            getMainProductDetails(productId, DataUtils.formatRequestParamAge("30", ""), 1, 1);
        }
    }

    public void onItemClick(int productId, ParamGetRisks param) {
        if (productId == 0) {
            submitMainChoose(null);
            return;
        }

        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getResponseTouDan(productId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBodyBase<ResponseTouDan>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBodyBase<ResponseTouDan> value) {
                        Log.d("wangwang","toudan " + JsonUtil.parseObjectToJson(value));
                        if (value != null && value.data != null){
                            touDanInfo = new TouDanInfo(value.data.isOpenInsurance, value.data.insuranceUrl,value.data.productId);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getMainProductDetails(productId, param);
                    }

                    @Override
                    public void onComplete() {
                        getMainProductDetails(productId, param);
                    }
                });
    }

    /**
     * 提交主险选中
     *
     */
    public void submitMainChoose(List<InsuranceEntity> list) {
        MainChooseEvent event = new MainChooseEvent();
        event.list = list;
        event.touDanInfo = new TouDanInfo(touDanInfo);
        touDanInfo = null;
        EventBus.getDefault().post(event);
        ActivityFinishStack.getInstance().finishAllElement();
    }

    public void getMainProductDetails(int productId, String age, int sex, int profession) {
        ProposalApi.getInstance(mContext).getMainDetails(productId, age, sex, profession, new OnResponseListener<ResponseMainDetail>() {
            @Override
            public void onSuccess(ResponseMainDetail body, ResponsePage page) {
                if (body == null) {
                    submitMainChoose(null);
                    return;
                }
                body.parseOptions();
                submitMainChoose(DataUtils.toInsuranceList(body));
            }

            @Override
            public void onFailure(ResponseError error) {
                submitMainChoose(null);
            }
        },(BaseRxActivity)mContext);
    }
}
